import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ahj_app/base/utils/permission/PerUtils.dart';
import 'package:flutter_ahj_app/base/utils/permission/permissionGroup.dart';
import '../ToastUtils.dart';
import 'package:permission_handler/permission_handler.dart';

class ChoiceLocationUtils {
  static MethodChannel _kChannel =
      MethodChannel('com.qingning.qn/ChoiceLocationUtils');//flutter调用原生通道  默认地址address可传可不传
  ///原生调用flutter通道 返回数据格式如下
  ///{
  ///   province:省name
  ///   city:市name
  ///   ad:行政区name
  ///   snippet:地址信息
  ///   title:名称
  ///   lng:经度
  ///   lat:纬度
  ///}
  static MethodChannel methodChannel =
      MethodChannel("com.qingning.qn/ChoiceLocationUtilsResult");

  /// address 打开地图默认的定位位置 不传默认为定位位置
  static choice(BuildContext context, {Key key, String address}) async {
    String result = "2"; //默认无权限
    if (context != null) {
      // 判断是否有定位权限
      result =
          await PerUtils.checkPermission(context, [Permission.location]);
    }
    if (result == "1") {
      if (address == null) {
        address = "";
      }

      Map<String, dynamic> map = Map();
      map["address"] = address;
      try {
        await _kChannel.invokeMethod('choice_location', map);
      } on PlatformException catch (e) {
        ToastUtils.showToast(e.message ?? '获取地理位置失败');
      }
    }
  }
}
